/*******************************************************************************
 * Copyright (c) 2012, 2013 XML-Print Project.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     XML-Print Project - initial API and implementation
 ******************************************************************************/
package info.xmlprint.core.referencesystem;

import java.io.Serializable;
import java.util.HashMap;

import org.dom4j.Element;

public class ReferenceField extends AbstractBaseItem implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5187056794463670980L;

	@Override
	public void init() {
		valueMap = new HashMap<String, String>();

		nodeNameMap = new HashMap<String, String>();
		nodeNameMap.put("id", "id");
		nodeNameMap.put("name", "name");
		nodeNameMap.put("content", "content");
		nodeNameMap.put("content-select", "content-select");
		nodeNameMap.put("content-value-format", "content-value-format");
		nodeNameMap.put("content-value-typo", "content-value-typo");
		nodeNameMap.put("content-xPath", "content-xPath");
		nodeNameMap.put("counter", "counter");
		nodeNameMap.put("counter-select", "counter-select");
		nodeNameMap.put("counter-attribute", "counter-attribute");
		nodeNameMap.put("counter-startValue", "counter-startValue");
		nodeNameMap.put("reset", "reset");
		nodeNameMap.put("reset-select", "reset-select");
		nodeNameMap.put("reset-name", "reset-name");
		nodeNameMap.put("reset-xPath", "reset-xPath");
		nodeNameMap.put("title", "title");
		nodeNameMap.put("title-select", "title-select");
		nodeNameMap.put("title-attribute", "title-attribute");
		nodeNameMap.put("title-xPath", "title-xPath");

		// set all value="";
		for (String k : nodeNameMap.keySet()) {
			valueMap.put(k, "");
		}
	}

	@Override
	public void setRootElementName() {
		rootElementName = "field";
	}

	@Override
	public void updateByElement(Element elem) {
		if (elem == null || !elem.getName().equalsIgnoreCase(rootElementName))
			return;

		readNodeValue(elem, new String[] { "id", "name" });

		//Node content = elem.selectSingleNode("content");
		readNodeValue(elem, new String[] { "content-select",
				"content-value-format", "content-value-typo", "content-xPath" });

		//Node counter = elem.selectSingleNode("counter");
		readNodeValue(elem, new String[] { "counter-select",
				"counter-attribute", "counter-startValue" });

		//Node reset = elem.selectSingleNode("reset");
		readNodeValue(elem, new String[] { "reset-select", "reset-name",
				"reset-xPath" });

		//Node title = elem.selectSingleNode("title");
		readNodeValue(elem, new String[] { "title-select", "title-attribute",
				"title-xPath" });

	}
}
